import { Component, Injector } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { BasePage } from "../../basePage";
import { ActionSheetController } from 'ionic-angular';
import { nailerService } from '../../../providers/nailerService';
import { StorageService } from '../../../providers/StorageService';
import {Camera, CameraOptions} from "@ionic-native/camera";
import {ImagePicker, ImagePickerOptions} from "@ionic-native/image-picker";
/**
 * Generated class for the NotePubPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-note-pub',
  templateUrl: 'note-pub.html',
})
export class NotePubPage extends BasePage {
  msg: '';
  avatar:'';
  user_info: any;
  nailerName='选择栏目';
  /**发布文章参数 */
  publishObj={
    article_title:'',
    article_content:'',
    article_tag:0,
    presenter_type:0,
    article_images:[],
    user_id:0
  }
  constructor(injector: Injector,public actionSheetCtrl: ActionSheetController,public nailerservice:nailerService,public storage:StorageService,public imagePicker: ImagePicker, public camera: Camera) {
    super(injector);
  }
  ionViewDidLoad() {
    console.log('ionViewDidLoad NotePubPage');
    this.user_info= this.storage.read('userinfo');
    this.publishObj.presenter_type = this.user_info['user_type'];
    this.publishObj.user_id = this.user_info['user_id'];
    console.log(this.user_info);
  }

  /**点击返回到上一页面 */
  dismiss(){
    let data = { 'foo': 'bar' };
    this.viewCtrl.dismiss(data);
  }
  presentToast() {
    let toast = this.toastCtrl.create({
      message: this.msg,
      duration: 2000,
      position: 'top'
    });
    toast.onDidDismiss(() => {
      console.log('Dismissed toast');
    });
  
    toast.present();
  }
  /**点击发布帖子*/
  async doPub() {
    console.log(this.publishObj);
    let res =await this.nailerservice.publishNearArticle(this.publishObj);
    if(res.error_code != 0){
      this.msg = res.reason;
      this.presentToast();
    }else if(res.error_code == 0){
      this.msg = res.msg;
      this.presentToast();
    }
    console.log(res);
  }
 /**查看图片 */
//  scaleModalScalePage(){
//   this.modalCtrl.create(NotePubPage, {}, {
//     enterAnimation: 'modal-scale-enter',
//     leaveAnimation: 'modal-scale-leave'
//   }).present();
// }
presentActionSheet() {
  let actionSheet = this.actionSheetCtrl.create({
    title: '选择所属栏目',
    buttons: [
      {
        text: '美甲',
        handler: () => {
         this.nailerName = "美甲栏目";
         this.publishObj.article_tag = 1;
        }
      },{
        text: '美睫',
        handler: () => {
          this.nailerName = "美睫栏目";
          this.publishObj.article_tag = 2;
        }
      },
      
      
      {
        text: '纹绣',
        handler: () => {
          this.nailerName = "纹绣栏目";
          this.publishObj.article_tag = 3;
        }
      },{
        text: '微整形',
        handler: () => {
          this.nailerName = "微整形栏目";
          this.publishObj.article_tag = 4;
        }
      },
      
      
      {
        text: '取消',
        role: 'cancel',
        handler: () => {
          console.log('Cancel clicked');
        }
      }
    ]
  });
  actionSheet.present();
}
presentActionSheetCamr() {
  let actionSheet = this.actionSheetCtrl.create({
    title: '选择',
    buttons: [
      {
        text: '拍照上传',
        handler: () => {
        
        }
      },{
        text: '从相册上传',
        handler: () => {
         
        }
      },
    
      {
        text: '取消',
        role: 'cancel',
        handler: () => {
          console.log('Cancel clicked');
        }
      }
    ]
  });
  actionSheet.present().then(value => {
    return value;
  });
}


takePhoto() {
  const options: CameraOptions = {
    quality: 100,
    allowEdit: true,
    targetWidth: 200,
    targetHeight: 200,
    saveToPhotoAlbum: true,
  };

  this.camera.getPicture(options).then(image => {
    console.log('Image URI: ' + image);
    this.avatar = image.slice(7);
  }, error => {
    console.log('Error: ' + error);
  });
}

chooseFromAlbum() {
  const options: ImagePickerOptions = {
    maximumImagesCount: 1,
    width: 200,
    height: 200
  };
  this.imagePicker.getPictures(options).then(images => {
    if (images.length > 1) {
      this.presentAlert();
    } else if (images.length === 1) {
      console.log('Image URI: ' + images[0]);
      this.avatar = images[0].slice(7);
    }
  }, error => {
    console.log('Error: ' + error);
  });
}

presentAlert() {
  let alert = this.alertCtrl.create({title: "上传失败", message: "只能选择一张图片作为头像哦", buttons: ["确定"]});
  alert.present().then(value => {
    return value;
  });
}

}
